package com.ruoyi.commodity.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Oem;
import com.ruoyi.common.core.domain.ProductCates;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.commodity.mapper.OemMapper;
import com.ruoyi.commodity.service.IOemService;

/**
 * 分类管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-13
 */
@Service
public class OemServiceImpl implements IOemService 
{
    @Autowired
    private OemMapper oemMapper;

    /**
     * 查询分类管理
     * 
     * @param id 分类管理主键
     * @return 分类管理
     */
    @Override
    public Oem selectOemById(Long id)
    {
        return oemMapper.selectOemById(id);
    }

    /**
     * 查询分类管理列表
     * 
     * @param oem 分类管理
     * @return 分类管理
     */
    @Override
    @DataScope(oemAlias = "o")
    public List<Oem> selectOemList(Oem oem)
    {
        return oemMapper.selectOemList(oem);
    }

    @Override
    public List<TreeSelect> selectOemTree(Oem oem) {
        List<Oem> oemList = SpringUtils.getAopProxy(this).selectOemList(oem);
        return buildOemTreeSelect(oemList);
    }


    @Override
    public List<Oem> buildOemTree(List<Oem> oemList) {
        List<Oem> returnList = new ArrayList<>();
        List<Long> collect = oemList.stream().map(Oem::getId).collect(Collectors.toList());
        for (Oem oem : oemList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(oem.getParentId())){
                recursionFn(oemList,oem);
                returnList.add(oem);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = oemList;
        }
        return returnList;
    }

    private void recursionFn(List<Oem> oemList , Oem oem){
        // 得到子节点列表
        List<Oem> childList = getChildList(oemList,oem);
        oem.setChildrenOem(childList);
        for (Oem oems : childList)
        {
            if (hasChild(oemList, oems))
            {
                recursionFn(oemList, oems);
            }
        }

    }

    @Override
    public List<TreeSelect> buildOemTreeSelect(List<Oem> oemList) {
        List<Oem> oemTree = buildOemTree(oemList);
        return oemTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 获得子节点
     * @param oemList
     * @param oem
     * @return
     */
    private List<Oem> getChildList(List<Oem> oemList, Oem oem){
        List<Oem> returnList = new ArrayList<>();
        Iterator<Oem> iterator = oemList.iterator();
        while (iterator.hasNext()){
            Oem n = (Oem) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == oem.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Oem> oemList, Oem oem)
    {
        return getChildList(oemList, oem).size() > 0;
    }
}
